﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL.Sys;
using Model.Sys;
using PagedList;
using Model.Organization;
using BLL.Organization;
using Common;
using Enums;
using Enums.Sys;
namespace DAGG.Controllers.Sys
{
    public class SysUserController : BaseController
    {
        SysUserBLL bll = new SysUserBLL();
        EnterpriseBLL bll0 = new EnterpriseBLL();
        Sys_RoleBLL roleBll = new Sys_RoleBLL();
        //
        // GET: /SysUser/
        public ActionResult Index(string term = "", int page = 1)
        {
            SysUserModel userModel = base.GetUserInfo();
            var enterId = userModel.EnterpriseId;
            string strWhere = string.Empty;
            //递归获取其所属子公司和子公司的子公司
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == userModel.RoleId)//管理员角色 查看全部
                strWhere += "  ";
            else
            {
                var listWhere = string.Join(",", bll0.GetSonEnterprise(userModel.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (su.EnterpriseId  in (0))";
                else
                    strWhere += " and (su.EnterpriseId in (" + listWhere + "))";
            }


            //根据登录用户机构得到该机构下的所有用户信息
            IPagedList<SysUserModel> sysUserList = bll.GetList(strWhere, term).ToPagedList(page, 10);
            ViewBag.MyPageList = sysUserList;
            return View();
        }
        public ActionResult Edit(int id = 0)
        {
            SysUserModel model = new SysUserModel();
            if (id > 0) model= bll.GetModelById(id.ToString());//编辑 
            SysUserModel userModel = base.GetUserInfo();
            var enterId = userModel.EnterpriseId;
            string strWhere = string.Empty;
            //递归获取其所属子公司和子公司的子公司
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == userModel.RoleId)//管理员角色 查看全部
                strWhere += "  ";
            else
            {
                var listWhere = string.Join(",", bll0.GetSonEnterprise(userModel.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (oe.ID  in (0))";
                else
                    strWhere += " and (oe.ID in (" + listWhere + "))";
            }

            //绑定公司下拉框
            List<EnterpriseModel> list = bll0.GetList(strWhere,"");
            List<SelectListItem> selectListItem = new List<SelectListItem>();

            foreach (var item in list)
            {
                bool Istrue = false;
                if (item.ID == model.EnterpriseId) Istrue = true;
                selectListItem.Add(new SelectListItem { Text = item.EnterpriseName, Value = item.ID.ToString(), Selected = Istrue });
            }
            ViewData["selectList"] = selectListItem;

            string strWhere0 = string.Empty;
            //递归获取其所属子公司和子公司的子公司
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == userModel.RoleId)//管理员角色 查看全部
                strWhere0 += "  ";
            else
            {
                var listWhere0 = string.Join(",", bll0.GetSonEnterprise(userModel.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere0))
                    strWhere0 += " and (EnterpriseId  in (0))";
                else
                    strWhere0 += " and (EnterpriseId in (" + listWhere0 + "))";
            }
            //绑定角色下拉框
            List<Sys_RoleModel> roleList = roleBll.Getlist(strWhere0);
            List<SelectListItem> selectRodeListItem = new List<SelectListItem>();

            foreach (var item in roleList)
            {
                bool Istrue = false;
                if (item.ID == model.RoleId) Istrue = true;
                selectRodeListItem.Add(new SelectListItem { Text = item.RoleName, Value = item.ID.ToString(), Selected = Istrue });
            }
            ViewData["selectRoleList"] = selectRodeListItem;
            if(model!=null&&model.ID!=0){
                model.Pwd =EDS3.DESDeCode(model.Pwd);
            }
            return View(model);
        }

        public ActionResult SaveData(SysUserModel model) {
            ResultEntity entity;
            SysUserModel sysModel =base.GetUserInfo();
            if (model.ID==0)//新增
            {
                model.CreateDate = DateTime.Now;
                model.CreateLoginID = sysModel.ID;
                model.CreateLoginName = sysModel.LoginName;
                model.ChangeDate = DateTime.Now;
                model.ChangeLoginID = sysModel.ID;
                model.ChangeLoginName = sysModel.LoginName;
                model.ViewDel = EnumManagement.GetEnumValue(SysEnum.NoDele);
                model.ViewFlag = 1;
                entity = bll.AddModel(model);
            }
            else
            {
                model.ChangeDate = DateTime.Now;
                model.ChangeLoginID = sysModel.ID;
                model.ChangeLoginName = sysModel.LoginName;
                entity = bll.Update(model);
            }
            return Redirect("/SysUser/Index");
        }
	}
}